/**
* @class
* Works with a bizproc to show the detailed viewed of a given bizobj using templates and the templating engine
* available in the app framework.
* This widget can work with a collection of templates from the template engine and switch 
* which template it is using based on the runtime, such as if the user is in an edit mode
* versus read only. 
<p>This widget will work with the Kusog startup factory using the <b>ksg-bizObjDisplay</b> css class
on the dom element this is attached to.  The following is an example of how to use this widget
on a web page.  The <b>ksg-ui</b> is the general css class used to be picked up by the startup factory.</p>
<pre>
&lt;div class="ksg-ui ksg-bizObjDisplay" 
	data-options='{"bizProcId":"art1"}'
	data-template='{"readOnly":"ArtIdImageTitleBody", "edit":"ArtIdImageTitleBody-edit"}'&gt;
&lt;/div&gt;
</pre>
<p>You can also manually create the widget with javascript code similar to the following:</p>
<pre>
$("#myDisplayId").bizObjDisplay({
	bizProcId:"art1",
	template: {
		readOnly: "ArtIdImageTitleBody",
		edit: "ArtIdImageTitleBody-edit"
	}
});
</pre>
* @meta-type jQuery widget
* @Depends ksg.baseWidget.js
*/
ksg.ui.widgets.BizObjDisplay = $.extend({},ksg.ui.widgets.BaseWidget, /** @lends ksg.ui.widgets.BizObjDisplay# */{
	options: {
	},
	
	_create: function() {
		this._IWidget_construct();
	},
	
	destroy: function() {
		this._IWidget_destruct();
	},

	//IWidget
	getWidgetType: function() {return ksg.ui.BizProcWidgetType.Disp;},
	
	
	//--- IBizObjListDisplay
	displayBizObj: function(bizObj) {
		var self = this;
		ksg.ui.getTemplate(self._getTemplateName(), "", function(json) {
			self.element.html("");
			var obj = {
				isNotEmpty:function(name) {
					var x = ksg.bizobj.BizObj.getValue(this.data, name, "");
					return x.toString().length != 0;
				},
				gt:function(name) {
					return ksg.bizobj.BizObj.getValue(this.data, name, "");
				}
			};
			json.tmpl.tmpl($.extend({},obj,bizObj)).appendTo(self.element);
			
			//This should not have to be here, but is needed to hook up field edit listeners
			//This should be a  part of a bigger template handling system that would setup widgets 
			//in the template better
			setupListeners();
		});	
	},
	
	updateBizObj: function(bizObj) {
		this._getUpdatesFromChildElements(this.element, bizObj);
	},
	
	_getUpdatesFromChildElements: function($el, bizObj) {
		var self = this;
		$.each($el.children(), function() {
			var $child = $(this),
				className = null, fieldName = null, fieldType = null;
				
			if((className = self._findClassBeginingWith($child, "ksg-field-")) && (fieldType = ksg.ui.fields[className]) && (fieldName = $child.data("fieldname")))
				bizObj[fieldName] = fieldType.getVal($child);
			
			if(!$child.hasClass("ksg-ui"))
				self._getUpdatesFromChildElements($child, bizObj);
		});
	}
	
});
$.widget("ksg.bizObjDisplay", ksg.ui.widgets.BizObjDisplay);

ksg.ui.widgetMap["ksg-bizObjDisplay"] = function ($el, options) {
	$el.bizObjDisplay(options); 
};
